#ifndef APPAlgorithm_H
#define APPAlgorithm_H

#include "AdaptiveAlgorithmBase.h"

// Identyfikator janka
#include "SimEngine/RmnkSolver.h"
#include "SimEngine/WrmnkARXWrapper.h"

// Klasa wielomianu
#include "SimEngine/Polynomial.h"

namespace RafMat
{
namespace SimEngine
{

class APPAlgorithm : public AdaptiveAlgorithmBase
{
public:
    APPAlgorithm(RSTController& owner);

    bool setParameter(const std::string& param, const std::string& value) throw(std::logic_error);

    std::string getParameter(const std::string& param) const throw(std::logic_error);

    void adapt(double ctrlVar, double processVar, double setPoint);

    void reset();

    virtual SimFile::ConfigWriter& dumpParameters(SimFile::ConfigWriter& o) const;

private:
    static const std::string CONF_AMPOLY;
    static const std::string CONF_ADEGREE;
    static const std::string CONF_BDEGREE;
    static const std::string CONF_BPOLY;
    static const std::string CONF_KDELAY;

    static const double ESTB_TOLERANCE;

    void resetWrnmnk();
    void resizeBuffers();
    void cleanBuffers();

    void setAMPoly(const Polynomial& amPoly);
    //void setRDegree(unsigned int ndR);
    //void setSDegree(unsigned int ndS);
    void setADegree(unsigned int ndA);
    void setBPoly(const Polynomial& bpoly);
    void setBDegree(unsigned int ndB);
    void setKDelay(unsigned int k);

    std::deque<double> m_ymem;      ///< Pamięć próbek y
    std::deque<double> m_umem;      ///< Pamięć próbek u
    std::vector<double> m_ufmem;    ///< Pamięć próbek uf
    std::vector<double> m_yfmem;    ///< Pamięć próbek yf

    Polynomial m_AmPoly;

    Polynomial m_bpoly;
    //Polynomial m_rb;
    //Polynomial m_sb;

    unsigned m_k;                        ///< Opóźnienie
    unsigned m_ndR;
    unsigned m_ndS;
    unsigned m_ndA;
    unsigned m_ndB;
    //unsigned m_ndRB;
    //unsigned m_ndSB;

    RmnkSolverSPtr m_wrmnk;        ///< Obiekt do identyfikacji RB i SB
    RmnkSolver::Matrix m_theta;
    RmnkSolver::Matrix m_phi;

    WrmnkARXWrapperSPtr m_modelWrmnk;

    int m_adaptDelay;
};


}
}

#endif // APPAlgorithm_H
